package tools;

import javax.naming.*;


import java.sql.*;

import javax.sql.*;

public class DBManager {
	private static InitialContext context = null;
	
	public static Connection getConnection() throws SQLException, NamingException{
		if(context == null){
			context = new InitialContext();
		}
		
		DataSource dataSource = (DataSource)context.lookup("java:comp/env/jdbc/OnlineShop");
		return dataSource.getConnection();
	}
	
	//Get a Scrollable Statement
	public static Statement getStatement(Connection conn) throws SQLException{
		Statement stmt = conn.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
		return stmt;
	}
	
	public static void close(Connection conn){
		if(conn != null){
			try{
				conn.close();
			} catch (Exception ignored){
				
			}
		}
	}
	
	public static void close(Statement stmt){
		if(stmt != null){
			try{
				stmt.close();
			} catch (Exception ignored){
				
			}
		}
	}
	
	public static void close(PreparedStatement pstmt){
		if(pstmt != null){
			try{
				pstmt.close();
			} catch (Exception ignored){
				
			}
		}
	}
	
	public static void close(ResultSet rs){
		if(rs != null){
			try{
				rs.close();
			} catch (Exception ignored){
				
			}
		}
	}
}
